package action;

import bean.Course;
import bean.Student;

import com.opensymphony.xwork2.ActionSupport;

import dao.CourseDAO;
import dao.CourseDAOImpl;
import dao.StudentDAO;
import dao.StudentDAOImpl;
import tool.HibernateSessionFactory;

import java.util.List;
import java.util.Objects;

public class CourseAction extends ActionSupport {
    private static final long serialVersionUID = -4455580857268079530L;
    private StudentDAO studentDAO = new StudentDAOImpl(HibernateSessionFactory.getSession());
    private CourseDAO courseDAO = new CourseDAOImpl(HibernateSessionFactory.getSession());
    private List<Student> students;
    private Course course;
    private List<Course> courses;

    public List<Student> getStudents() {
        return students;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public List<Course> getCourses() {
        return courses;
    }

    @Override
    public String execute() throws Exception {
        students = studentDAO.queryAll();
        courses = courseDAO.queryAll();
        return super.execute();
    }

    public String add() {
        return courseDAO.add(course) ? "add" : null;
    }

    public String edit() {
        return Objects.nonNull(course = courseDAO.query(course.getStudentId())) ? "edit" : null;
    }

    public String modify() {
        return courseDAO.modify(course) ? "modify" : null;
    }

    public String remove() {
        return courseDAO.remove(course.getStudentId()) ? "remove" : null;
    }
}
